import App from './App'



// #ifndef VUE3
import Vue from 'vue'
// 如此配置即可

function isLogin(isAuth = false) {
	const token = uni.getStorageSync('sc-token');
	if (isAuth && !token) {
		// uni.showModal({
		// 	title: '提示',
		// 	content: '你还没有登录，是否前去登录?',
		// 	confirmText: '前往',
		// 	success: function(res) {
		// 		if (res.confirm) {
		uni.navigateTo({
			url: '/pages/login/index',
		})
		// 		}
		// 	}
		// });
		return false;
	} else {
		return true
	}
}

Vue.prototype.$isLogin = isLogin;

import uniAppConfig from './config'
Vue.prototype.$imgUrl = (url) => {
	uni.setStorageSync('imgUrl', uniAppConfig.url)
	return uniAppConfig.url + url
}

function goPage(res, type = 'navigateTo', isAuth = false) {
	console.log(res, type, isAuth);
	try {
		if (isAuth && isLogin(true)) {
			uni[type]({
				...res
			});
		} else if (!isAuth) {
			uni[type]({
				...res
			});
		}
	} catch (e) {
		console.log(e);
		// error
	}
};

Vue.prototype.$goPage = goPage;

// 预览图片
Vue.prototype.$previewImage = (index, urls = []) => {
	uni.previewImage({
		urls,
		current: index,
		indicator: 'default',
		loop: true
	})
}

// 提示
Vue.prototype.$success = (title, icon = 'none') => {
	uni.showToast({
		title,
		duration: 2000,
		icon
	});
};

// 拨打电话
Vue.prototype.$makePhoneCall = (phone) => {
	console.log('111');
	uni.makePhoneCall({
		phoneNumber: phone //仅为示例
	});
}

// 富文本图片最大100%  正则追加max-width
Vue.prototype.$textImageConvert = (content) => {
	// return content.replace(/<(img).*?(\/>|<\/img>)/g, function(mats) {
	// 	if (mats.indexOf('style') < 0) {
	// 		return mats.replace(/<\s*img/, '<img style="max-width:100%;height:auto;"');
	// 	} else {
	// 		return mats.replace(/style=("|')/, 'style=$1max-width:100%;height:auto;')
	// 	}
	// });
	return content.replace(/<img\s+[^>]*?>/gi, function(match) {
		var styleAttr = '';
		if (!match.includes('style=')) {
			styleAttr = ' style="max-width:100%;height:auto;"';
		} else {
			styleAttr = match.replace(/style=(["'])(.*?)\1/i, function(styleMatch, p1, p2) {
				if (!/\bmax-width\b/i.test(p2) || !/\bheight\b/i.test(p2)) {
					return ' style=' + p1 + p2 + ' max-width:100%;height:auto;' + p1;
				}
				return styleMatch;
			});
		}
		return match.replace(/\/?>/, styleAttr);
	});
}

// 轮播图导向
Vue.prototype.$goBannerPath = ({
	path,
	id,
	path_type
}, isAuth = false) => {
	const type = path_type.toString();
	console.log(type, path);
	switch (type) {
		case '1': // 富文本
			goPage({
				url: '/pages/global/agreement?actType=1&type=' + id
			}, 'navigateTo', isAuth)
			break;
		case '2': // 导向公众号链接
			goPage({
				url: '/pages/index/web-view?url=' + path
			}, 'navigateTo', isAuth)
			break;
		case '3': // 导向小程序
			uni.navigateToMiniProgram({
				appId: path,
				path: '',
				success(res) {
					// 打开成功
				}
			})
			break;
		case '4': // 导向内部链接
			goPage({
				url: path
			}, 'navigateTo', isAuth)
			break;
		case '5': // 跳到视频号
			const { openType, finderUserName } = parseQueryString(path);  
			if (openType == 'home') {
				wx.openChannelsUserProfile({
					finderUserName,
					success: (res) => {
						console.log(res, '打开视频号成功');
					},
					fail: (res) => {
						console.log(res, '打开视频号失败');
					},
				})
			} else {
				wx.openChannelsLive({
					finderUserName,
					feedId,
					nonceId,
					success: (res) => {
						console.log(res, '打开视频号直播间成功');
					},
					fail: (res) => {
						console.log(res, '打开视频号直播间失败');
					},
				})
			}
			break;
		default:
			break;
	}
}

function parseQueryString(queryString) {  
  const queryPairs = queryString.split('&');  
  const result = {};  
  queryPairs.forEach(pair => {  
    const [key, value] = pair.split('=');  
    result[decodeURIComponent(key)] = decodeURIComponent(value || '');  
  });  
  return result;  
} 

/**
 * 打开未知
 * @param {Object} latitude 纬度
 * @param {Object} longitude 经度
 * @param {Object} name 位置名
 */
Vue.prototype.$openLocation = (latitude, longitude, name, address) => {
	console.log(latitude, longitude, name, address);
	uni.openLocation({
		latitude: Number(latitude),
		longitude: Number(longitude),
		name,
		address,
		success: function() {
			console.log('success');
		},
		fail: (info) => {
			uni.showToast({
				title: '权限未开启',
				duration: 2000,
				icon
			});
		}
	});
}



import './uni.promisify.adaptor'
// Vue.config.productionTip = false


import api from '@/common/vmeitime-http/'
// 全局挂载后使用
Vue.prototype.$api = api


import share from "./utils/share.js" // 分享文件 文件位置看具体目录
Vue.mixin(share)

// 全局SVG挂载
import {
	changeSvgColor
} from '@/utils/globalMethods.js'
Vue.prototype.$gm = changeSvgColor



import blCusSvg from '@/components/bl-cus-svg/bl-cus-svg.vue';
Vue.component('bl-cus-svg', blCusSvg);

import blBannerImg from '@/components/baseUI/bl-banner-img/index.vue';
Vue.component('bl-banner-img', blBannerImg);

import blBannerVideo from '@/components/baseUI/bl-banner-video/index.vue';
Vue.component('bl-banner-video', blBannerVideo);

import blBottomFixed from "@/components/baseUI/bl-bottom-fixed/index.vue";
Vue.component('bl-bottom-fixed', blBottomFixed);

import hansTabber from '@/components/hans-tabbar/hans-tabbar.vue'
Vue.component('hans-tabber', hansTabber);

// 自适应图片图片高度 宽度100%
import blAdaptationImg from '@/components/baseUI/bl-adaptation-img/index.vue'
Vue.component('bl-adaptation-img', blAdaptationImg);

App.mpType = 'app'
const app = new Vue({
	...App,
	share
})
app.$mount()
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif